layui.use(['form', 'table', 'fsoft'], function () {
	var $ = layui.$
		, form = layui.form
		, table = layui.table
		, fsoft = layui.fsoft;
	var tableId = "table";
	var searchFormId = "fsoftForm";
	//字典列表刷新
	table.render({
		elem: '#' + tableId, method: 'post', toolbar: '#fsoft-table-toolbar',
		url: layui.cache['contentPath'] + '/sys-dict/findItemList',
		where: form.val(searchFormId),
		cols: [[
			{ type: 'checkbox', fixed: 'left' },
			{ field: 'code', title: '配置编码', width: 180, sort: true },
			{ field: 'name', title: '配置名称', width: 360, sort: true },
			{ field: 'dictName', title: '所属字典', width: 150 },
			{ field: 'status', title: '状态', width: 100, align: 'center', templet: '#switchStatus' },
			{ field: 'sortNo', title: '排序', width: 80, align: 'center' },
			{ fixed: 'right', width: 150, align: 'center', title: '操作', toolbar: '#itemFormBar', fixed: 'right' }
		]],
		page: true,
		limit: 10,
		limits: [10, 20, 50, 100],
		height: 'full-108'
	});
	//搜索监听
	form.on('submit(sreach)', function (data) {
		table.reload('table', { where: data.field });
		return false;
	});
	/* 工具条 */
	table.on('toolbar(fsoftItemTable)', function (obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'add':
				frmActiveAction.add();
				break;
			case 'removeBatch':
				frmActiveAction.removeBatch(checkStatus.data);
				break;
			case 'enable':
				frmActiveAction.enable(checkStatus.data);
				break;
			case 'disable':
				frmActiveAction.disable(checkStatus.data);
				break;
		};
	});

	//工具栏（操作列）
	table.on('tool(fsoftItemTable)', function (obj) {
		var data = obj.data;
		if (obj.event === 'edit') {
			layer.open({
				id: "sysitems-modify-form"
				, type: 2
				, title: "编辑配置项"
				, area: ['500px', '300px']
				, shadeClose: false
				, content: layui.cache['contentPath'] + '/sys-dict/edititem/' + data.id
				, end: function () {
					frmActiveAction.doSearch();
				}
			});
		} else if (obj.event === 'del') {
			frmActiveAction.removeBatch([data]);
		}
	});
	//状态
	form.on('switch(status)', function (data) {
		var msg = "启用成功";
		var submitType = "enableItem";
		if (data.elem.checked == false) {
			msg = "禁用成功";
			submitType = "disableItem";
		}
		var ids = new Array();
		ids.push(this.value);
		fsoft.put(layui.cache['contentPath'] + '/sys-dict/' + submitType, ids, function (rs) {
			if (rs.code == 0) {
				layer.msg(msg, { icon: 1, time: 2000 });
				table.reload('table', { where: data.field });
			} else {
				layer.msg(rs.msg, { icon: 2, time: 2000 });
			}
		});

	});
	/* 工具条按钮事件监听 */
	var frmActiveAction = {
		doSearch: function (params) {
			if (params == undefined || params == null)
				params = $.extend({}, form.val(searchFormId));
			else
				$.extend(params, form.val(searchFormId));
			table.reload(tableId, { where: params });
		}
		, removeBatch: function (data) { //批量删除
			if (data == undefined || data.length == 0) {
				fsoft.warnMsg("请至少选择一条记录");
				return;
			}
			var batchIds = new Array();
			$.each(data, function (idx, item) {
				batchIds.push(item.id);
			});
			var msg = "确认要删除当前字典";
			if (batchIds.length > 1) {
				msg = "确认要删除已选中的（" + batchIds.length + "）条记录？";
			}
			fsoft.confirm("询问", msg, function (index) {
				fsoft.delete(layui.cache['contentPath'] + '/sys-dict/removeItem', batchIds, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("删除成功");
						frmActiveAction.doSearch();
					} else {
						fsoft.errorMsg(rs.msg);
					}
				});
				layer.close(index);
			});
		},
		add: function () {
			layer.open({
				id: "sysitems-modify-form"
				, type: 2
				, title: "添加配置项"
				, area: ['500px', '300px']
				, shadeClose: false
				, content: layui.cache['contentPath'] + '/sys-dict/additem/' + $("#dictId").val()
				, end: function () {
					frmActiveAction.doSearch();
				}
			});
		},
		enable: function (data) {/*启用*/
			if (data == undefined || data.length == 0) {
				fsoft.warnMsg("请至少选择一条记录");
				return;
			}
			var batchIds = new Array();
			$.each(data, function (idx, item) {
				if (item["status"] == 0)
					batchIds.push(item.id);
			});
			if (batchIds.length != data.length) {
				fsoft.warnMsg("已选中的字典项，存在已启用的配置项，请重新选择！");
				return false;
			}
			fsoft.confirm("询 问", "确认要启用已选中的（" + batchIds.length + "）个配置项？", function (index) {
				fsoft.put(layui.cache['contentPath'] + '/sys-dict/enableItem', batchIds, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("启用成功！");
						frmActiveAction.doSearch();
					} else {
						fsoft.errorMsg(rs.msg);
					}
					layer.close(index);
				});
			});
		},
		disable: function (data) { /*禁用*/
			if (data == undefined || data.length == 0) {
				layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
				return;
			}
			var batchIds = new Array();
			$.each(data, function (idx, item) {
				if (item["status"] == 1)
					batchIds.push(item.id);
			});
			if (batchIds.length != data.length) {
				fsoft.warnMsg("已选中的字典项，存在已禁用的配置项，请重新选择！");
				return false;
			}
			fsoft.confirm("询 问", "确认要禁用已选中的（" + batchIds.length + "）个配置项？", function (index) {
				fsoft.put(layui.cache['contentPath'] + '/sys-dict/disableItem', batchIds, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("禁用成功");
						frmActiveAction.doSearch();
					} else {
						fsoft.errorMsg(rs.msg);
					}
					layer.close(index);
				});
			});
		}
	};
});